package com.cloud.common.mybatis.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration;
import jakarta.annotation.PostConstruct;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * Author:  xuanning
 * Date:    2023/2/20 17:44
 * 整合 mybatis-plus的拦截器和pageHelper拦截器的执行顺序
 * 先执行 mybatis-plus的拦截器
 */
@Configuration
@AutoConfigureAfter(PageHelperAutoConfiguration.class)
public class MybatisFilterConfiguration {

    @Autowired
    private List<SqlSessionFactory> sqlSessionFactories;

    @Autowired
    private  MybatisPlusInterceptor mybatisPlusInterceptor;

    @PostConstruct
    public void addInterceptor(){
        for(SqlSessionFactory sqlSessionFactory:sqlSessionFactories){
            org.apache.ibatis.session.Configuration configuration=sqlSessionFactory.getConfiguration();
            configuration.addInterceptor(mybatisPlusInterceptor);
        }
    }
}
